package com.boc.langxiang.presenter.shalo;


import com.boc.langxiang.contract.shalo.ShaloListContract;
import com.boc.langxiang.entity.request.ShaLoListRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.ShaloListContent;
import com.boc.langxiang.entity.response.ShaloListEntity;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class ShaloListPresenter implements ShaloListContract.Presenter {

    private final ApiService mService;
    private ShaloListContract.View mView;

    public ShaloListPresenter(ShaloListContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }


    @Override
    public void shaList(int page) {
        mView.showLoading();
        ShaLoListRequest request = new ShaLoListRequest();
        request.page = page;
        Observable<BaseResponse<ShaloListContent>> register = mService.shaList(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShaloListContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShaloListContent> response) {
                        ShaloListContent shaloListContent = response.getContent();
                        if(shaloListContent!=null){
                            List<ShaloListEntity> shaloListEntities = shaloListContent.getRes();
                            mView.shaListSuccess(shaloListEntities);
                        }
                    }
                });
    }
}
